﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Coder.Framework;

namespace Coder.IdeOne
{
    public class Soap
    {
        Credentials Credentials { get; set; }

        static string ByteCodeURL = "http://www.bytecode.ca/coder/submit.php?user=";
        bool LoginEnabled { get; set; }
        WebClient _webClient;
        /// <summary>
        /// Sets up the IdeOne Byte Code Canada communications.
        /// </summary>
        /// <param name="credentials">An optional argument.</param>
        public Soap(Credentials credentials)
        {
            _webClient = new WebClient();
        }

        /// <summary>
        ///        project: represents the Project object with appropriate variables.
        ///        callback: the object called when a value is returned. Gets a string with the response info.
        ///        The IdeOne SOAP interface expects the following things in the parameters:
        ///        (string) url, (string) method, (string) user, (string) pass, (string) code, (int) language, (string) input, (bool) run, (bool) private mode
        ///
        ///        The Byte Code Canada PHP Interface runs as such:
        ///        www.bytecode.ca/coder/submit.php
        ///        And takes the following parameters:
        ///        login(bool) - If a user and pass is provided
        ///        user/pass - OPTIONAL: If the user specified their own credentials.
        ///        language - The language ID associated with the code.
        ///        code - an encoded URI with the program.
        ///        input - an encoded input for the program to use.
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public async Task<string> SubmitCode(Project project)
        {
            return await _webClient.RetrieveDocURL(ByteCodeURL + Credentials.UserName + "&pass=" + Credentials.UserAPIPass + "&login="
                + LoginEnabled + "&language=" + project.Language.LanguageID + "&code=" + EncodeUriComponent(project.Code) + "&input=" + EncodeUriComponent(project.Challenge.TestInput));
        }

        public string EncodeUriComponent(string originalString)
        {
            return Uri.EscapeUriString(originalString);
        }

    }
}
